import json

from re_common.baselibrary.utils.core.requests_core import MsgCode

__all__ = ["cnkijournal_cnkiarticle_m_list",
           "chaoxingjournal_chaoxingjournallist_m_list",
           "cnkijournal_cnkijournallist_m_list",
           "wanfangjournal_wanfangissue_m_list",
           "article_middlerwares",
           "article_middlerwares2",
           "ref_middlerwares",
           "ref_middlerwares2",
           "on_request_end",
           "cnki_journal_middlerwares",
           "cnki_cnkinet1st_middlerwares",
           "cnkijournal_cnkinet1st_m_list",
           "cnkijournal_cnkinet1starticle_m_list",
           "cnki_url_id_timeout_middlerwares",
           "cnkijournal_cnkiissue_m_list",
           "cnki_no_verification",
           "cnki_cnkiissue_middlerwares",
           "cnkijournal_cnkinet1stall_m_list",
           "wanfang_wanfangissue_middlerwares",
           "chaoxing_chaoxingissue_middlerwares",
           "chaoxingjournal_chaoxingissue_m_list",
           "cnkijournal_cnkinet1styear_m_list",
           "cnki_journal_yearlist_middlerwares",
           "cnkizl_no_data_middlerwares",
           "cnkipatent_cnkizlhome_m_list",
           "cnkipatent_cnkizllist_m_list",
           "cnkipatent_cnkizlarticle_m_list",
           "cnkiccndpaper_cnkipapersublist_m_list",
           "cnkiccndpaper_cnkipaperarticle_m_list",
           "cnkithesis_cnkithesissublist_m_list",
           "cnkithesis_cnkithesisarticle_m_list",
           "cnkicdfdthesis_cnkicdfdthesissublist_m_list",
           "cnkicdfdthesis_cnkicdfdthesisarticle_m_list",
           "cnkiconference_cnkiconferencesublist_m_list",
           "cnkiconference_cnkiconferencearticle_m_list",
           "cnkiipfdconference_cnkiipfdconferencesublist_m_list",
           "cnkiipfdconference_cnkiipfdconferencearticle_m_list",
           "chaoxingjournal_chaoxinghome_m_list",
           "chaoxingjournal_chaoxinghomeclass_m_list",
           "chaoxingjournal_chaoxingqkhomeclass_m_list",
           "cnkiimage_cnkiimagearticle_m_list",
           "article_image_3_middlerwares",
           "wanfang_search_middlerwares",
           "wanfangarticle_nodata_middlerwares",
           "wanfangthesis_wanfangthesis_search_list_m_list",
           "wanfangthesis_wanfangthesis_typelist_m_list",
           "wanfangthesis_wanfangthesis_yearlist_m_list",
           "wanfangthesis_wanfangthesis_search_article_m_list",
           "wanfangconference_wanfangconference_search_list_m_list",
           "wanfangconference_wanfangconference_typelist_m_list",
           "wanfangconference_wanfangconference_yearlist_m_list",
           "wanfangconference_wanfangconference_search_article_m_list",
           "wanfangunconference_wanfangunconference_search_article_m_list",
           "wanfangpatent_wanfangpatent_search_list_m_list",
           "wanfangpatent_wanfangpatent_typelist_m_list",
           "wanfangjournal_wanfangarticle_m_list",
           "baiduxueshu_baiduxuehome_m_list",
           "baiduxueshu_baiduxuehome_middlerwares",
           "baiduxueshu_baiduxuelist_m_list",
           "baiduxueshulist2_marks_middlerwares",
           "article_image_1_middlerwares",
           "chaoxing_chaoxingjournallist_middlerwares",
           "cnkijournal_cnkiqkclasshome_m_list",
           "cnki_cnkiqkclasshome_middlerwares",
           "cnkijournal_cnkiqkhome_m_list",
           "cnki_cnkiqkhome_middlerwares",
           ]

# m_list 变量 遵循 task_name + _ + task_tag + _m_list
wanfangthesis_wanfangthesis_search_list_m_list = ["wanfang_search_middlerwares"]
wanfangthesis_wanfangthesis_typelist_m_list = ["wanfang_search_middlerwares"]
wanfangthesis_wanfangthesis_yearlist_m_list = ["wanfang_search_middlerwares"]
wanfangconference_wanfangconference_search_list_m_list = ["wanfang_search_middlerwares"]
wanfangconference_wanfangconference_typelist_m_list = ["wanfang_search_middlerwares"]
wanfangconference_wanfangconference_yearlist_m_list = ["wanfang_search_middlerwares"]
wanfangpatent_wanfangpatent_search_list_m_list = ["wanfang_search_middlerwares"]
wanfangpatent_wanfangpatent_typelist_m_list = ["wanfang_search_middlerwares"]
chaoxingjournal_chaoxingjournallist_m_list = ["chaoxing_need_dama", "chaoxing_chaoxingjournallist_middlerwares"]
cnkijournal_cnkiarticle_m_list = ["article_middlerwares",
                                  "article_middlerwares2",
                                  "ref_middlerwares",
                                  "ref_middlerwares2",
                                  "on_request_end",
                                  "cnki_url_id_timeout_middlerwares",
                                  "cnki_no_verification"]
cnkithesis_cnkithesisarticle_m_list = ["article_middlerwares",
                                       "article_middlerwares2",
                                       "ref_middlerwares",
                                       "ref_middlerwares2",
                                       "on_request_end",
                                       "cnki_url_id_timeout_middlerwares",
                                       "cnkizl_no_data_middlerwares",
                                       "cnki_no_verification"]
cnkiconference_cnkiconferencearticle_m_list = ["article_middlerwares",
                                               "article_middlerwares2",
                                               "ref_middlerwares",
                                               "ref_middlerwares2",
                                               "on_request_end",
                                               "cnki_url_id_timeout_middlerwares",
                                               "cnkizl_no_data_middlerwares",
                                               "cnki_no_verification"]
cnkiipfdconference_cnkiipfdconferencearticle_m_list = ["article_middlerwares",
                                                       "article_middlerwares2",
                                                       "ref_middlerwares",
                                                       "ref_middlerwares2",
                                                       "on_request_end",
                                                       "cnki_url_id_timeout_middlerwares",
                                                       "cnkizl_no_data_middlerwares",
                                                       "cnki_no_verification"]
cnkicdfdthesis_cnkicdfdthesisarticle_m_list = ["article_middlerwares",
                                               "article_middlerwares2",
                                               "ref_middlerwares",
                                               "ref_middlerwares2",
                                               "on_request_end",
                                               "cnki_url_id_timeout_middlerwares",
                                               "cnkizl_no_data_middlerwares",
                                               "cnki_no_verification"]

cnkijournal_cnkiqkclasshome_m_list = ["cnki_cnkiqkclasshome_middlerwares"]
cnkijournal_cnkiqkhome_m_list = ["cnki_cnkiqkhome_middlerwares"]


# 用于cnkijournallist 级
cnkijournal_cnkijournallist_m_list = ["cnki_journal_middlerwares", "cnki_journal_yearlist_middlerwares"]
cnkijournal_cnkinet1st_m_list = ["cnki_cnkinet1st_middlerwares"]
cnkijournal_cnkinet1stall_m_list = ["cnki_cnkinet1st_middlerwares"]
cnkijournal_cnkinet1styear_m_list = ["cnki_cnkinet1st_middlerwares"]

cnkipatent_cnkizlhome_m_list = ["cnkizl_no_data_middlerwares"]

cnkipatent_cnkizllist_m_list = ["cnkizl_no_data_middlerwares"]

cnkipatent_cnkizlarticle_m_list = ["cnkizl_no_data_middlerwares"]

cnkiccndpaper_cnkipapersublist_m_list = ["cnkizl_no_data_middlerwares"]
cnkiccndpaper_cnkipaperarticle_m_list = ["cnkizl_no_data_middlerwares"]

cnkithesis_cnkithesissublist_m_list = ["cnkizl_no_data_middlerwares"]

cnkicdfdthesis_cnkicdfdthesissublist_m_list = ["cnkizl_no_data_middlerwares"]

cnkiconference_cnkiconferencesublist_m_list = ["cnkizl_no_data_middlerwares"]
cnkiipfdconference_cnkiipfdconferencesublist_m_list = ["cnkizl_no_data_middlerwares"]
cnkijournal_cnkinet1starticle_m_list = ["article_middlerwares", "article_middlerwares2",
                                        "cnki_url_id_timeout_middlerwares"]
cnkijournal_cnkiissue_m_list = ["cnki_url_id_timeout_middlerwares", "cnki_cnkiissue_middlerwares"]

wanfangjournal_wanfangissue_m_list = ["wanfang_wanfangissue_middlerwares"]

chaoxingjournal_chaoxingissue_m_list = ["chaoxing_chaoxingissue_middlerwares", "chaoxing_need_dama"]
chaoxingjournal_chaoxinghome_m_list = ["chaoxing_need_dama"]
chaoxingjournal_chaoxinghomeclass_m_list = ["chaoxing_need_dama"]
chaoxingjournal_chaoxingqkhomeclass_m_list = ["chaoxing_need_dama"]

cnkiimage_cnkiimagearticle_m_list = ["article_image_3_middlerwares", "article_image_1_middlerwares"]

wanfangjournal_wanfangarticle_m_list = ["wanfangarticle_nodata_middlerwares"]
wanfangthesis_wanfangthesis_search_article_m_list = ["wanfangarticle_nodata_middlerwares"]
wanfangconference_wanfangconference_search_article_m_list = ["wanfangarticle_nodata_middlerwares"]
wanfangunconference_wanfangunconference_search_article_m_list = ["wanfangarticle_nodata_middlerwares"]

baiduxueshu_baiduxuehome_m_list = ["baiduxueshu_baiduxuehome_middlerwares"]
baiduxueshu_baiduxuelist_m_list = ["baiduxueshulist2_marks_middlerwares"]


def article_middlerwares(self):
    """
    验证返回码
    :return:
    """
    if self.html.find('所查找的文献不存在') != -1 or self.html.find("未找到相关数据") != -1:
        self.resp_dicts["code"] = MsgCode.PARE_NO_DATA
        self.resp_dicts["msg"] = "所查找的文献不存在 or 未找到相关数据"
        return False, self.resp_dicts
    return True, self.resp_dicts


def article_middlerwares2(self):
    """
    验证返回码
    :return:
    """
    if self.html.find('所查找的文件不存在') != -1:
        self.resp_dicts["code"] = MsgCode.NO_RESOURCE
        self.resp_dicts["msg"] = "所查找的文件不存在"
        return False, self.resp_dicts
    return True, self.resp_dicts


def ref_middlerwares(self):
    """
    验证返回码
    :return:
    """
    if self.html.find('未找到相关数据') != -1:
        self.resp_dicts["code"] = MsgCode.NO_RESOURCE
        self.resp_dicts["msg"] = "未找到相关数据"
        return False, self.resp_dicts
    return True, self.resp_dicts


def ref_middlerwares2(self):
    """
    :return:
    """
    if self.html.find('无标题页') != -1:
        self.resp_dicts["code"] = MsgCode.NO_RESOURCE
        self.resp_dicts["msg"] = "无标题页"
        return False, self.resp_dicts
    return True, self.resp_dicts


def on_request_end(self):
    if self.html:
        self.html = self.html.replace("'", '"')  # 单引号替换为双引号
    return True, self.resp_dicts


def cnki_journal_middlerwares(self):
    """
        验证返回码
        :return:
    """
    if self.html.find('暂无此期刊') != -1:
        self.resp_dicts["code"] = MsgCode.PARE_NO_DATA
        self.resp_dicts["msg"] = "暂无此期刊"
        return False, self.resp_dicts
    return True, self.resp_dicts


def cnki_journal_yearlist_middlerwares(self):
    """
        验证返回码
        :return:
    """
    if self.html.find('暂无数据') != -1:
        self.resp_dicts["code"] = MsgCode.PARE_NO_DATA
        self.resp_dicts["msg"] = "暂无数据"
        return False, self.resp_dicts
    return True, self.resp_dicts


def cnki_cnkinet1st_middlerwares(self):
    """
        验证返回码
        :return:
    """
    if self.html.find('暂无目录信息') != -1:
        self.resp_dicts["code"] = MsgCode.PARE_NO_DATA
        self.resp_dicts["msg"] = "所查找的网络首发显示暂无目录信息"
        return False, self.resp_dicts
    return True, self.resp_dicts


def cnki_url_id_timeout_middlerwares(self):
    """
    验证返回码
    :return:
    """
    if self.html.find('系统检测到您的访问行为异常，请帮助我们完成 <em>验证</em>') != -1:
        self.resp_dicts["code"] = MsgCode.URL_TIMEOUT
        self.resp_dicts["msg"] = "url因超时失效"
        return False, self.resp_dicts
    return True, self.resp_dicts


def cnki_cnkiqkclasshome_middlerwares(self):
    if self.html.find('共 <em></em> 条结果') != -1:
        self.resp_dicts["code"] = MsgCode.PARE_NO_DATA
        self.resp_dicts["msg"] = "当前页无数据"
        return False, self.resp_dicts
    return True, self.resp_dicts

def cnki_cnkiqkhome_middlerwares(self):
    if self.html.find('共 <em></em> 条结果') != -1:
        self.resp_dicts["code"] = MsgCode.PARE_NO_DATA
        self.resp_dicts["msg"] = "当前页无数据"
        return False, self.resp_dicts
    return True, self.resp_dicts


def cnki_no_verification(self):
    '''
    cnki取消验证跳过的页面
    :param self:
    :return:
    '''
    if self.html.find('></script><script src=') != -1:
        self.resp_dicts["code"] = MsgCode.NO_RESOURCE
        self.resp_dicts["msg"] = "无引证和引文"
        return False, self.resp_dicts
    return True, self.resp_dicts


def cnkizl_no_data_middlerwares(self):
    '''
        cnki专利学科查询结果检测
        :param self:
        :return:
    '''
    if self.html.find('暂无分组结果') != -1 or self.html.find("暂无数据") != -1:
        self.resp_dicts["code"] = MsgCode.PARE_NO_DATA
        self.resp_dicts["msg"] = "无查询数据"
        return False, self.resp_dicts
    return True, self.resp_dicts


def cnki_cnkiissue_middlerwares(self):
    """
        验证返回码
        :return:
    """
    if self.html.find('暂无目录信息') != -1:
        self.resp_dicts["code"] = MsgCode.PARE_NO_DATA_ERR
        self.resp_dicts["msg"] = "所查找的期显示暂无目录信息,可能是真正无目录,也可能是issue_value过期"
        return False, self.resp_dicts
    return True, self.resp_dicts


def wanfang_wanfangissue_middlerwares(self):
    """
        验证返回码
        :return:
    """
    if self.html.find('暂无数据') != -1:
        self.resp_dicts["code"] = MsgCode.PARE_NO_DATA
        self.resp_dicts["msg"] = "所查找的期显示暂无数据"
        return False, self.resp_dicts
    return True, self.resp_dicts


def chaoxing_chaoxingissue_middlerwares(self):
    """
        验证返回码
        :return:
    """
    if self.html.find('"dxids":""') != -1:
        self.resp_dicts["code"] = MsgCode.PARE_NO_DATA
        self.resp_dicts["msg"] = "所查找的期显示暂无数据"
        return False, self.resp_dicts
    return True, self.resp_dicts


def chaoxing_chaoxingjournallist_middlerwares(self):
    """
        验证返回码
        :return:
    """
    if self.html.find('《》') != -1:
        self.resp_dicts["code"] = MsgCode.PARE_NO_DATA
        self.resp_dicts["msg"] = "该刊已下架"
        return False, self.resp_dicts
    return True, self.resp_dicts


def chaoxing_need_dama(self):
    """
        验证返回码, 判定是否打码, 目前打码返回未知, 暂时写个结构放着
        :return:
    """
    if self.html.find('thirdLogin') != -1 or self.html.find("社交账号登录") != -1:
        self.resp_dicts["code"] = 557
        self.resp_dicts["msg"] = "需要验证码"
        return False, self.resp_dicts
    return True, self.resp_dicts


def wanfangarticle_nodata_middlerwares(self):
    """
        验证返回码
        :return:
    """
    if self.html.find('{"extradataMap": {"Status": "ERROR"}') != -1:
        self.resp_dicts["code"] = MsgCode.PARE_NO_DATA
        self.resp_dicts["msg"] = "空白页，无数据"
        return False, self.resp_dicts
    return True, self.resp_dicts


def article_image_3_middlerwares(self):
    """
    验证返回码
    :return:
    """
    if self.html.find('对不起，该图片文献页信息不存在') != -1:
        self.resp_dicts["code"] = MsgCode.PARE_NO_DATA
        self.resp_dicts["msg"] = "对不起，该图片文献页信息不存在"
        return False, self.resp_dicts
    return True, self.resp_dicts


def article_image_1_middlerwares(self):
    """
    验证返回码
    :return:
    """
    if self.html.count('对不起，未加载到相关图片信息！') >= 2:
        self.resp_dicts["code"] = 1104
        self.resp_dicts["msg"] = "对不起，未加载到相关图片信息！"
        return False, self.resp_dicts
    return True, self.resp_dicts


def wanfang_search_middlerwares(self):
    """
        验证返回码
        :return:
    """
    if "result" not in self.html and "聚类异常" not in self.html:
        print("-3--", self.html)
        self.resp_dicts["code"] = MsgCode.PARE_NO_DATA
        self.resp_dicts["msg"] = "查询无数据"
        return False, self.resp_dicts
    return True, self.resp_dicts


def baiduxueshu_baiduxuehome_middlerwares(self):
    """
        验证返回码
        :return:
    """
    self.html = json.dumps(json.loads(self.html), ensure_ascii=False)
    if self.html.find('未检索到任何结果') != -1:
        self.resp_dicts["code"] = MsgCode.PARE_NO_DATA
        self.resp_dicts["msg"] = "所查找的name不存在"
        return False, self.resp_dicts
    return True, self.resp_dicts


def baiduxueshulist2_marks_middlerwares(self):
    if self.html.strip() == "":
        return True, self.resp_dicts
    else:

        if self.html.find("合作学者") > -1:
            return True, self.resp_dicts
        else:
            self.resp_dicts["code"] = MsgCode.MARK_ERROR
            self.resp_dicts["msg"] = "mark Feature err: {}".format("合作学者")
            return False, self.resp_dicts
